﻿using Digitalmes.Infrastructure.Scada;
using Digitalmes.Infrastructure.Scada.Configuration;
using Digitalmes.Infrastructure.Scada.Forwarders;
using Digitalmes.Infrastructure.Scada.Handlers;
using ThingsEdge.Router;

namespace Digitalmes.Infrastructure;

public static class ScadaServiceCollectionExtensions
{
    /// <summary>
    /// 添加 SCADA 采集器。
    /// </summary>
    /// <param name="hostBuilder"></param>
    public static void AddScada(IHostBuilder hostBuilder)
    {
        hostBuilder.AddThingsEdgeRouter(builder =>
        {
            builder.UseDeviceFileProvider()
                .UseDeviceHeartbeatForwarder<NativeHeartbeatForwarder>()
                .UseNativeNoticeForwarder<NativeNoticeForwarder>()
                .UseNativeRequestForwarderHandler<NativeRequestForwarderHandler>()
                .AddEventBusRegisterAssembly(typeof(ScadaServiceCollectionExtensions).Assembly)
                .UseOpsProvider();
        });

        hostBuilder.ConfigureServices((context, services) =>
        {
            services.Configure<ScadaConfig>(context.Configuration.GetSection("Scada"));

            // 启动项
            services.AddHostedService<ScadaStartupHostedService>();

            // 注册 <see cref="AbstractHandler"/> 应用服务。
            services.RegisterForType<IScadaHandler>(typeof(ScadaServiceCollectionExtensions).Assembly);
        });
    }
}
